#include "board.h"
#include "RFM70.h"
#include <stdio.h>

#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)

void send(s16 temperature) {
	if (txTimeout()) {
		flushTxFIFO();
		cliTimeout();
	}
	sendPayload2((uint8_t *) &temperature, 2);
}

const uint8_t x[][2] = {
// address data
		{ (0x20 | 0x00), 0x0F }, //Disable CRC ,CRC=1byte, POWER UP, TX
		{ (0x20 | 0x01), 0x3F }, //Enable auto acknowledgement data pipe0-5
		{ (0x20 | 0x02), 0x3F }, //Enable RX Addresses pipe0-5
		{ (0x20 | 0x03), 0x03 }, //RX/TX address field width 5byte
		{ (0x20 | 0x04), 0x08 }, //x = 250 ms = 4000ms, y = 15 tries
		{ (0x20 | 0x05), 0x17 }, //channel = 0x17
		{ (0x20 | 0x06), 0x3F }, //air data rate-2M,out power 5dbm,setup LNA gain high (0dBM)
		{ (0x20 | 0x07), 0x07 }, //
		{ (0x20 | 0x08), 0x00 }, //
		{ (0x20 | 0x09), 0x00 }, //
		{ (0x20 | 0x0C), 0xc3 }, //LSB Addr pipe 2
		{ (0x20 | 0x0D), 0xc4 }, //LSB Addr pipe 3
		{ (0x20 | 0x0E), 0xc5 }, //LSB Addr pipe 4
		{ (0x20 | 0x0F), 0xc6 }, //LSB Addr pipe 5
		{ (0x20 | 0x11), 0x20 }, //Payload len pipe0
		{ (0x20 | 0x12), 0x20 }, //Payload len pipe0
		{ (0x20 | 0x13), 0x20 }, //Payload len pipe0
		{ (0x20 | 0x14), 0x20 }, //Payload len pipe0
		{ (0x20 | 0x15), 0x20 }, //Payload len pipe0
		{ (0x20 | 0x16), 0x20 }, //Payload len pipe0
		{ (0x20 | 0x17), 0x20 }, //Payload len pipe0
		{ (0x20 | 0x1C), 0x3F }, //Enable dynamic payload legth data pipe0-5
		{ (0x20 | 0x1D), 0x07 } //Enables Dynamic Payload Length,Enables Payload with ACK
};

int32_t payload[RFM70_MAX_PACKET_LEN/4];

int main(void) {
	init();
	DBG(("\n\r init 1\n\r"));

	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_OCInitTypeDef TIM_OCInitStruct;

	TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV4;
	TIM_TimeBaseInitStruct.TIM_Period = 1000 - 1; // 0..999
	TIM_TimeBaseInitStruct.TIM_Prescaler = 24 - 1; // Div 240
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);

	TIM_OCStructInit(&TIM_OCInitStruct);
	TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
	TIM_OCInitStruct.TIM_Pulse = 10; // 0 .. 1000 (0=Always Off, 1000=Always On)
	TIM_OC2Init(TIM2, &TIM_OCInitStruct);
	TIM_OC3Init(TIM2, &TIM_OCInitStruct);
	TIM_OC4Init(TIM2, &TIM_OCInitStruct);
	TIM_Cmd(TIM2, ENABLE);

	setPower(0);
	Begin(-1);
	setModeRX();

	int r = 0;
	int g = 0;
	int b = 0;
	int state = 0;
	int delay = 3;
	for (;;) {
		if (state == 8) {
			if (b < 1000)
				b++;
			if (g < 1000)
				g++;
			if (b > 700 && g > 700) {
				state = 0;
			}
		}
		if (state == 7) {
			if (b > 0)
				b--;
			if (b == 0) {
				state = 8;
			}
		}
		if (state == 6) {
			if (g > 0)
				g--;
			if (g == 0) {
				state = 7;
			}
		}
		if (state == 5) {
			if (b < 1000)
				b++;
			if (b == 1000) {
				state = 6;
			}
		}
		if (state == 4) {
			if (r < 1000)
				r++;
			if (r == 1000) {
				state = 5;
			}
		}
		if (state == 3) {
			if (b > 0)
				b--;
			if (b == 0) {
				state = 4;
			}
		}
		if (state == 2) {
			if (g < 1000)
				g++;
			if (g == 1000) {
				state = 3;
			}
		}
		if (state == 1) {
			if (b < 1000)
				b++;
			if (b == 1000) {
				state = 2;
			}
		}
		if (state == 0) {
			if (r > 0)
				r--;
			if (g > 0)
				g--;
			if (b > 0)
				b--;
			if (r == 0 && g == 0 && b == 0) {
				state = 1;
			}
		}
		TIM2->CCR3 = g;
		TIM2->CCR4 = b;
		TIM2->CCR2 = r;
		delayMs(delay);

		uint8_t len = receivePayload((uint8_t *) payload);
		if (len) {
			printf("recv %x", payload[0]);
			state = 0xFF;
			int mode = payload[0]>>30;
			if(mode == 0){
				r = (payload[0]) & 0x3FF;
				b = (payload[0]>>10) & 0x3FF;
				g = (payload[0]>>20) & 0x3FF;
			}else if(mode == 1){
				delay = payload[0] & 0xFFFF;
			}else if(mode == 2){
				state = 0;
			}
		}
	}

	return 0;
}
